package io.jee.runshan.service.impl;

import io.jee.medusa.orm.DaoSupport;
import io.jee.medusa.service.impl.BaseService;
import io.jee.runshan.dao.IProductDao;
import io.jee.runshan.domain.Product;
import io.jee.runshan.service.IProductService;
import io.jee.runshan.utils.FileTool;
import io.jee.runshan.utils.Zip;

import java.io.File;
import java.io.FileInputStream;
import java.math.BigDecimal;
import java.util.UUID;

import javax.annotation.Resource;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

/**
 * 商品Service实现
 * @author SunX
 *
 */
@Service
@Transactional
public class ProductServiceImpl extends BaseService<Product> implements IProductService {
	
	@Resource
	private IProductDao productDao;

	@Override
	public DaoSupport<Product> getDao() {
		return productDao;
	}
	
	@Override
	@Async
	public void importProduct(File inputZip, String tempPath, String path, String realPath, String contextPath) {
		String errRow = null;
		try {
		new Zip().unZip(inputZip, tempPath);
		File xls = FileTool.getExcel(new File(tempPath));
		System.out.println(xls.getPath());
		
		Workbook wb = null;  
        if (xls.getPath().endsWith(".xls")) {  
            wb = new HSSFWorkbook(new FileInputStream(xls));  
        } else if (xls.getPath().endsWith(".xlsx")) {  
            wb = new XSSFWorkbook(new FileInputStream(xls));  
        }
        
        Sheet sheet1 = wb.getSheetAt(0);
        boolean hasTitle = true;
        for (Row row : sheet1) {
        	if(hasTitle){
        		hasTitle = false;
        		continue;
        	}
        	String NO = row.getCell(0).getStringCellValue();
        	if(StringUtils.isEmpty(NO)){
        		break;
        	}
        	errRow = NO;
        	String productName = row.getCell(1)==null?null:row.getCell(1).getStringCellValue();
        	String productBrief = row.getCell(2)==null?null:row.getCell(2).getStringCellValue();
        	double marketPrice = row.getCell(3).getNumericCellValue();
        	double salesMoney = row.getCell(4).getNumericCellValue();
        	double stock = row.getCell(5).getNumericCellValue();
        	
        	Product product = new Product();
        	product.setProductCode(NO);
        	product.setProductName(productName);
        	product.setProductBrief(productBrief);
        	product.setMarketPrice(new BigDecimal(marketPrice));
        	product.setSalesPrice(new BigDecimal(salesMoney));
        	product.setStock((int)stock);

        	
        	
			File top = FileTool.getTopImage(new File(tempPath), NO);
			int i = 1;
			for (File file : top.listFiles()) {
				if(file.isDirectory()){
					continue;
				}
				if(i>5){
					break;
				}
				String suffix = file.getName().substring(file.getName().lastIndexOf("."));
				if(!suffix.equals(".jpg")&&!suffix.equals(".gif")&&!suffix.equals(".png")){
					continue;
				}
				String filename = UUID.randomUUID().toString()+suffix;
				File t = new File(realPath, filename);
				FileTool.copyFile(file, t);
				switch (i) {
				case 1:
					product.setImage1(path+"/"+filename);
					break;
				case 2:
					product.setImage2(path+"/"+filename);
					break;
				case 3:
					product.setImage3(path+"/"+filename);
					break;
				case 4:
					product.setImage4(path+"/"+filename);
					break;
				case 5:
					product.setImage5(path+"/"+filename);
					break;
				default:
					break;
				}
				i++;
			}
			File image = FileTool.getImage(new File(tempPath), NO);
			StringBuffer detail = new StringBuffer();
			for (File file : image.listFiles()) {
				if(file.isDirectory()){
					continue;
				}
				String suffix = file.getName().substring(file.getName().lastIndexOf("."));
				if(!suffix.equals(".jpg")&&!suffix.equals(".gif")&&!suffix.equals(".png")){
					continue;
				}
				String filename = UUID.randomUUID().toString()+suffix;
				File t = new File(realPath, filename);
				FileTool.copyFile(file, t);
				String src = contextPath+path+"/"+filename;
				detail.append("<p><img alt=\"\" src=\""+src+"\"></p>");
			}
			product.setDetail(detail.toString());
			this.save(product);
        }
	} catch (Exception e) {
		e.printStackTrace();
		System.err.println("到："+errRow+"的时候报错");
	} finally {
		try {
			FileTool.deletefile(tempPath);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	}

}
